package com.langsheng.lsintell.ui.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.LinearLayout;

import com.langsheng.lsintell.R;
import com.langsheng.lsintell.ui.fragment.LSInternetFragment;
import com.langsheng.lsintell.ui.fragment.LSMineFragment;
import com.langsheng.lsintell.ui.fragment.LSShopFragment;
import com.langsheng.lsintell.ui.fragment.LSStoreFragment;
import com.langsheng.lsintell.ui.fragment.LSVipPwdFragment;
import com.langsheng.lsintell.ui.widget.bottombar.BottomBarLayout;
import com.langsheng.lsintell.ui.widget.bottombar.TabEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xxw on 2017/2/23.
 */

public class LSMainActivity extends LSBaseActivity {

    private final static int NAV_COLOR_SELECT = Color.parseColor("#FF880D");
    private final static int NAV_COLOR_DEFAULT = Color.parseColor("#767676");


    @BindView(R.id.ll_fragment_switcher)
    LinearLayout llFragmentSwitcher;
    @BindView(R.id.bbl_bottomBar)
    BottomBarLayout bblBottomBar;

    private ArrayList<Fragment> fragments;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initFragments();
        createBottomView();
        showFragment(0);
    }

    /**
     * 创建底部导航栏
     */
    private void createBottomView() {
        List<TabEntity> entityList = new ArrayList<>();
        TabEntity tab1 = new TabEntity();
        tab1.setNormalIconId(R.drawable.nav_password);
        tab1.setSelectIconId(R.drawable.nav_password_sel);
        tab1.setText(getString(R.string.nav_vip_password));
        TabEntity tab2 = new TabEntity();
        tab2.setNormalIconId(R.drawable.nav_store);
        tab2.setSelectIconId(R.drawable.nav_store_sel);
        tab2.setText(getString(R.string.nav_store));
        TabEntity tab3 = new TabEntity();
        tab3.setNormalIconId(R.drawable.nav_shopping);
        tab3.setSelectIconId(R.drawable.nav_shopping_sel);
        tab3.setText(getString(R.string.nav_shopping));
        TabEntity tab4 = new TabEntity();
        tab4.setNormalIconId(R.drawable.nav_internet);
        tab4.setSelectIconId(R.drawable.nav_internet_sel);
        tab4.setText(getString(R.string.nav_internet));
        TabEntity tab5 = new TabEntity();
        tab5.setNormalIconId(R.drawable.nav_mine);
        tab5.setSelectIconId(R.drawable.nav_mine_sel);
        tab5.setText(getString(R.string.nav_mine));
        entityList.add(tab1);
        entityList.add(tab2);
        entityList.add(tab3);
        entityList.add(tab4);
        entityList.add(tab5);
        bblBottomBar.setNormalTextColor(NAV_COLOR_DEFAULT);
        bblBottomBar.setSelectTextColor(NAV_COLOR_SELECT);
        bblBottomBar.setTabList(entityList);
        bblBottomBar.setOnItemClickListener(new BottomBarLayout.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                showFragment(position);
            }
        });
        bblBottomBar.showTab(0, bblBottomBar.getChildAt(0));
    }

    private void showFragment(int index) {
        if (fragments != null) {
            if (index < fragments.size()) {
                FragmentManager fm = getSupportFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                Fragment fragment = fragments.get(index);
                if (!fragment.isAdded()) {
                    ft.add(R.id.ll_fragment_switcher, fragment);
                }
                ft.replace(R.id.ll_fragment_switcher, fragment);
                ft.commitAllowingStateLoss();
            }
        }
    }

    private void initFragments() {
        fragments = new ArrayList<>();
        fragments.add(LSVipPwdFragment.newInstance());
        fragments.add(LSShopFragment.newInstance());
        fragments.add(LSStoreFragment.newInstance());
        fragments.add(LSInternetFragment.newInstance());
        fragments.add(LSMineFragment.newInstance());
    }
}
